//
//  SettingsScene.m
//  jumpingfrog
//
//  Created by Tri Vo on 8/16/13.
//  Copyright 2013 Tri Vo. All rights reserved.
//

#import "SettingsScene.h"
#import "GeneralDef.h"
#import "GameController.h"
#import "MenuScene.h"
#import "SceneManager.h"

@implementation SettingsScene

+ (CCScene *) scene {
    CCScene *scene = [CCScene node];
    SettingsScene *layer = [SettingsScene node];
    [scene addChild:layer];
    return scene;
}

- (id)init {
    if (self = [super init]) {
        [self initScreen];
    }
    return self;
}

- (void)dealloc {
    [super dealloc];
}

#pragma mark - Scene methods

- (void) initScreen {
    CGSize winSize = [GameController getWinSize];
    CCSprite *pBackground = [CCSprite spriteWithFile:IMG_BACKGROUND];
    [pBackground setAnchorPoint:CGPointZero];
    [pBackground setPosition:CGPointZero];
    [self addChild:pBackground];
    
    // Title
    CCLabelTTF *pLblTitle = [CCLabelTTF labelWithString:STR_SETTINGS fontName:FONT_TITLE fontSize:28];
    [pLblTitle setPosition:CGPointMake(winSize.width/2, winSize.height/2 + 125)];
    [self addChild:pLblTitle];
    
    // Menu
    /// Sound
    CCMenuItemFont *pSoundON = [CCMenuItemFont itemWithString:STR_SOUND_ON target:nil selector:nil];
    CCMenuItemFont *pSoundOFF = [CCMenuItemFont itemWithString:STR_SOUND_OFF target:nil selector:nil];
    CCMenuItemToggle *pToggleSound = nil;
    if ([GameController getSoundState]) {
        pToggleSound = [CCMenuItemToggle itemWithTarget:self selector:@selector(btnSound_Clicked) items:pSoundON, pSoundOFF, nil];
    } else {
        pToggleSound = [CCMenuItemToggle itemWithTarget:self selector:@selector(btnSound_Clicked) items:pSoundOFF, pSoundON, nil];
    }

    /// Music
    CCMenuItemFont *pMusicON = [CCMenuItemFont itemWithString:STR_MUSIC_ON target:nil selector:nil];
    CCMenuItemFont *pMusicOFF = [CCMenuItemFont itemWithString:STR_MUSIC_OFF target:nil selector:nil];
    CCMenuItemToggle *pToggleMusic = nil;
    if ([GameController getMusicState]) {
        pToggleMusic = [CCMenuItemToggle itemWithTarget:self selector:@selector(btnMusic_Clicked) items:pMusicON, pMusicOFF, nil];
    } else {
        pToggleMusic = [CCMenuItemToggle itemWithTarget:self selector:@selector(btnMusic_Clicked) items:pMusicOFF, pMusicON, nil];
    }
    
    CCMenu *pMenu = [CCMenu menuWithItems:pToggleSound, pToggleMusic, nil];
    [pMenu alignItemsVerticallyWithPadding:10];
    [pMenu setPosition:CGPointMake(winSize.width/2, winSize.height/2)];
    [self addChild:pMenu];
    
    // Btn back
    CCMenuItemFont *pBtnBack = [CCMenuItemFont itemWithString:STR_BACK target:self selector:@selector(btnBack_Clicked)];
    CCMenu *pMenuBack = [CCMenu menuWithItems:pBtnBack, nil];
    [pMenuBack setPosition:CGPointMake(50, 30)];
    [self addChild:pMenuBack];
}

#pragma mark - Scene actions

- (void) btnBack_Clicked {
    [[SceneManager getInstance] goMenu];
}

- (void) btnSound_Clicked {
    if ([GameController getSoundState]) {
        [GameController setSoundState:NO];
    } else {
        [GameController setSoundState:YES];
    }
}

- (void) btnMusic_Clicked {
    if ([GameController getMusicState]) {
        [GameController setMusicState:NO];
    } else {
        [GameController setMusicState:YES];
    }
}

@end
